package com.huangdada.hfweather.mapper;

import com.huangdada.hfweather.bean.SJWeather;
import com.huangdada.hfweather.bean.Weather;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

/**
 * @Description: 天气获取Mapper接口
 * @PROJECT_NAME: hf-weather
 * @CLASS_NAME:
 * @Author: huanglibin
 * @Date: 2020-03-30 21:38
 * @Version: 1.0
 */
@Mapper
public interface WeatherMapper {

    /**
     * 查询所有天气实况
     * @return
     */
    @Select("select * from weather")
    public List<Weather> queryAll();

    /**
     * 保存天气实况信息
     * @param weather
     * @return
     */
    @Insert({"insert into weather(cid,location,lat,lon,parent_city,cnty,admin_area,tz,fl,tmp,wind_deg,wind_dir,wind_sc,wind_spd,loc) " +
            "values(#{cid},#{location},#{lat},#{lon},#{parentCity},#{cnty},#{adminArea},#{tz},#{fl},#{tmp},#{windDeg},#{windDir},#{windSc},#{windSpd},#{loc,jdbcType=TIMESTAMP})"})
    Integer saveWeatherInfo(Weather weather);

    @Insert({"INSERT INTO `sjweather` VALUES (#{id} , #{areaid} , #{namecn} , #{lat} , #{lon} , #{udateLoc} , #{status} , #{aqi} ,#{dailyWeather} , #{cdate} ,#{mdate} )"})
    Integer saveSJWeatherInfo(SJWeather weather);

    @Update("<script>"+
            "update `sjweather` set " +
            "<if test='areaid!=null and areaid!=\"\"'>areaid=#{areaid,jdbcType=VARCHAR} ,</if>"+
            "<if test='namecn!=null and namecn!=\"\"'>namecn=#{namecn,jdbcType=VARCHAR} ,</if>"+
            "<if test='lat!=null and lat!=\"\"'>lat=#{lat,jdbcType=VARCHAR} ,</if>"+
            "<if test='lon!=null and lon!=\"\"'>lon=#{lon,jdbcType=VARCHAR} ,</if>"+
            "<if test='udateLoc!=null'>udate_loc=#{udateLoc,jdbcType=TIMESTAMP} ,</if>"+
            "<if test='status!=null and status!=\"\"'>status=#{status,jdbcType=VARCHAR} ,</if>"+
            "<if test='aqi!=null and aqi!=\"\"'>aqi=#{aqi,jdbcType=VARCHAR} ,</if>"+
            "<if test='dailyWeather!=null and dailyWeather!=\"\"'>daily_weather=#{dailyWeather,jdbcType=VARCHAR} ,</if>"+
            "<if test='mdate!=null'>mdate=#{mdate,jdbcType=TIMESTAMP}</if>"+
            "where areaid = #{areaid,jdbcType=VARCHAR}"+
            "</script>")
    Integer updateSJWeatherInfo(SJWeather weather);

    @Select({"select * from sjweather where areaid = #{areaid} and namecn = #{namecn}"})
    @Results(id = "sjweatherMap",value = {
            @Result(id = true,column = "id",property = "id",jdbcType = JdbcType.INTEGER),
            @Result(column = "aqi",property = "aqi",jdbcType = JdbcType.VARCHAR),
            @Result(column = "areaid",property = "areaid",jdbcType = JdbcType.VARCHAR),
            @Result(column = "namecn",property = "namecn",jdbcType = JdbcType.VARCHAR),
            @Result(column = "lat",property = "lat",jdbcType = JdbcType.VARCHAR),
            @Result(column = "lon",property = "lon",jdbcType = JdbcType.VARCHAR),
            @Result(column = "status",property = "status",jdbcType = JdbcType.VARCHAR),
            @Result(column = "udate_loc",property = "udateLoc",jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "cdate",property = "cdate",jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "mdate",property = "mdate",jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "daily_weather",property = "dailyWeather",jdbcType = JdbcType.VARCHAR)
    })
    SJWeather getWeatherByAreaidAndNamecn(String areaid, String namecn);
}
